package com.yt.generator.engine.config;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 映射模板生成的配置
 *
 * @author fcy
 * @date 2019-04-15 13:45
 */
@Data
public class MapperConfig {

    private ContextConfig contextConfig;

    private String pathTemplate;
    private String mapperXmlPathTemplate;
    private String packageName;//包名称
    private List<String> imports;//所引入的包

    public void init() {
        ArrayList<String> imports = new ArrayList<>();
        imports.add("com.baomidou.mybatisplus.mapper.BaseMapper");
        if (StringUtils.isNotEmpty(contextConfig.getModuleName())){
            imports.add(contextConfig.getProjectBasePackageName() + ".models.dto." + contextConfig.getModuleName() + "." + contextConfig.getEntityName() + "DTO");
        } else {
            imports.add(contextConfig.getProjectBasePackageName() + ".models.dto." + contextConfig.getEntityName() + "DTO");
        }
        this.imports = imports;

        if (StringUtils.isNotEmpty(contextConfig.getModuleName())) {
            this.packageName = contextConfig.getProjectBasePackageName() + ".mapper." + contextConfig.getModuleName();
        } else {
            this.packageName = contextConfig.getProjectBasePackageName() + ".mapper";
        }

        //模板生成路径
        if (StringUtils.isNotEmpty(contextConfig.getModuleName())) {
            this.pathTemplate = "\\" + contextConfig.getProjectName() + "-server\\src\\main\\java\\" + contextConfig.getProjectBasePackageName().replaceAll("\\.", "\\\\") + "\\mapper\\" + contextConfig.getModuleName() + "\\{}Mapper.java";
            this.mapperXmlPathTemplate = "\\" + contextConfig.getProjectName() + "-server\\src\\main\\java\\" + contextConfig.getProjectBasePackageName().replaceAll("\\.", "\\\\") + "\\mapper\\" + contextConfig.getModuleName() + "\\{}Mapper.xml";
        } else {
            this.pathTemplate = "\\" + contextConfig.getProjectName() + "-server\\src\\main\\java\\" + contextConfig.getProjectBasePackageName().replaceAll("\\.", "\\\\") + "\\mapper\\{}Mapper.java";
            this.mapperXmlPathTemplate = "\\" + contextConfig.getProjectName() + "-server\\src\\main\\java\\" + contextConfig.getProjectBasePackageName().replaceAll("\\.", "\\\\") + "\\mapper\\{}Mapper.xml";
        }
    }

}
